package com.example.fushuang.balabala.activities;

import android.graphics.Color;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.design.widget.TabLayout;
import android.support.v4.view.GravityCompat;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.app.AppCompatDelegate;
import android.support.v7.widget.Toolbar;
import android.telephony.TelephonyManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.widget.Toast;

import com.example.fushuang.balabala.R;
import com.example.fushuang.balabala.adapter.CommenFragmentPagerAdapter;
import com.example.fushuang.balabala.fragment.BaseFragment;
import com.example.fushuang.balabala.fragment.CategoryFragment;
import com.example.fushuang.balabala.fragment.DiscoveryFragment;
import com.example.fushuang.balabala.fragment.FollowFragment;
import com.example.fushuang.balabala.fragment.LiveFragment;
import com.example.fushuang.balabala.fragment.MovieFragment;
import com.example.fushuang.balabala.fragment.RecommendFragment;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener, View.OnClickListener {
    @BindView(R.id.main_toolbar)
    Toolbar mToolbar;
    @BindView(R.id.activity_main_drawLayout)
    DrawerLayout mDrawerLayout;
    @BindView(R.id.main_viewPager)
    ViewPager mViewPager;
    @BindView(R.id.main_navigationView)
    NavigationView mNavigationView;
    @BindView(R.id.main_tabLayout)
    TabLayout mTabLayout;

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.option_menu, menu);
        return true;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
//        设置沉浸式状态栏

//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
//        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
//        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
//        getWindow().setStatusBarColor(Color.RED);
//        }

        //设置是否捕捉没有捕获的异常
        MobclickAgent.setCatchUncaughtExceptions(true);

        mNavigationView.setNavigationItemSelectedListener(this);
        View navigationViewHeaderView = mNavigationView.getHeaderView(0);
        navigationViewHeaderView.findViewById(R.id.main_navigationView_head_change_mode).setOnClickListener(this);

        setSupportActionBar(mToolbar);
        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);

        List<BaseFragment> list = new ArrayList<>();
        list.add(new LiveFragment());
        list.add(new RecommendFragment());
        list.add(new MovieFragment());
        list.add(new CategoryFragment());
        list.add(new FollowFragment());
        list.add(new DiscoveryFragment());

        mViewPager.setAdapter(new CommenFragmentPagerAdapter(getSupportFragmentManager(), list));
        mTabLayout.setupWithViewPager(mViewPager);


//        TelephonyManager manager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
////        IMEI
//        String deviceId = manager.getDeviceId();
//        //这个方法可以在线程意外终止的情况下,进行处理接口回调;
//        Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
//            @Override
//            public void uncaughtException(Thread t, Throwable e) {
//
//            }
//        });

    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
        MobclickAgent.onPageStart("main");
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
        MobclickAgent.onPageEnd("main");
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        switch (id) {
            case android.R.id.home:
                mDrawerLayout.openDrawer(GravityCompat.START, true);
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        int itemId = item.getItemId();
        switch (itemId) {
            case R.id.main_navigationView_home:
                Toast.makeText(this, "home", Toast.LENGTH_SHORT).show();
                break;
            case R.id.main_navigationView_myBigVip:
                Toast.makeText(this, "大会员", Toast.LENGTH_SHORT).show();
                break;
            case R.id.main_navigationView_offline:
                break;
            case R.id.main_navigationView_history:
                break;
            case R.id.main_navigationView_follow:
                break;
            case R.id.main_navigationView_shoucang:
                break;
            case R.id.main_navigationView_money:
                break;


        }
        return true;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.main_navigationView_head_change_mode:
                int mode = AppCompatDelegate.getDefaultNightMode();

                if (mode == AppCompatDelegate.MODE_NIGHT_NO) {
                    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
                    recreate();
                } else {
                    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
                    recreate();
                }
                break;

        }
    }

}
